package cn.tedu.luban.demand.webapi.service.impl;

import cn.tedu.luban.commons.pojo.demand.dto.RequestOrderDTO;
import cn.tedu.luban.commons.pojo.demand.dto.RequestOrderListDTO;
import cn.tedu.luban.commons.pojo.demand.model.AccessProvider;
import cn.tedu.luban.commons.pojo.demand.model.RequestOrder;
import cn.tedu.luban.commons.pojo.demand.vo.RequestOrderListItemVO;
import cn.tedu.luban.commons.pojo.demand.vo.RequestOrderVO;
import cn.tedu.luban.commons.restful.JsonPage;
import cn.tedu.luban.commons.utils.PropertiesUtils;
import cn.tedu.luban.demand.service.IRequestOrderService;
import cn.tedu.luban.demand.webapi.mapper.AccessProviderMapper;
import cn.tedu.luban.demand.webapi.mapper.RequestOrderMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

@Service
@Slf4j
@DubboService
public class RequestOrderServiceImpl implements IRequestOrderService {

    @Autowired
    private RequestOrderMapper requestOrderMapper;

    @Autowired
    private AccessProviderMapper accessProviderMapper;

    @Override
    public Long saveRequestOrder(RequestOrderDTO requestOrderDTO) {
        RequestOrder requestOrder = new RequestOrder();
        BeanUtils.copyProperties(requestOrderDTO, requestOrder);
        requestOrder.setRequestOrderNo(requestOrderDTO.getRequestOrderNo());
        requestOrder.setGmtCreate(System.currentTimeMillis());
        requestOrder.setGmtModified(requestOrder.getGmtCreate());
        requestOrder.setStatus(1);
        requestOrder.setCreateUserId(99L);
        requestOrder.setModifiedUserId(99L);
        requestOrder.setCreateUserName("root");
        requestOrder.setModifiedUserName("root");
        requestOrder.setProviderId(6);
        requestOrderMapper.insert(requestOrder);
        return requestOrder.getId();
    }

    @Override
    public JsonPage<RequestOrderListItemVO> queryRequestOrderList(RequestOrderListDTO requestOrderListDTO, Integer pageNo, Integer pageSize) {
        // 设置分页条件
        PageHelper.startPage(pageNo,pageSize);
        // 查询所有符合条件的需求单
        List<RequestOrder> list = requestOrderMapper.queryRequestOrders(requestOrderListDTO);

        // 将集合泛型转换为RequestOrderListItemVO类型
        List<RequestOrderListItemVO> voList=
                PropertiesUtils.copyListProperties(list,RequestOrderListItemVO.class);
        for (int i = 0; i < list.size(); i++) {
            // 取出集合中的元素
            RequestOrderListItemVO vo = voList.get(i);
            // 给未被赋值的属性赋值
            vo.setCategoryId(list.get(i).getOrderCategoryId());
            vo.setCreateTime(list.get(i).getGmtCreate());
            // 分润比例的获取,先根据当前RequestOrder对象的providerId获取商家信息
            AccessProvider accessProvider=accessProviderMapper
                    .getEntity(list.get(i).getProviderId()*1L);
            if(accessProvider !=null){
                // 获取分润比例
                BigDecimal profitScale=accessProvider.getProfitScale();
                // 根据分润比例计算工人师傅本单的金额
                Long workerAmount = getWorkerAmount(list.get(i).getOrderAmount(), profitScale);
                // 设置订单金额
                vo.setViewOrderAmount(workerAmount);
                vo.setProfitScale(profitScale);
            }
        }

        return JsonPage.restPage(new PageInfo<>(voList));
    }

    private Long getWorkerAmount(Long price, BigDecimal scale) {
        if (scale != null) {
            return new BigDecimal(price).multiply(new BigDecimal(100).subtract(scale)).divide(new BigDecimal(100)).longValue();
        }
        //默认20
        return new BigDecimal(price).multiply(new BigDecimal(20)).divide(new BigDecimal(100)).longValue();
    }



    @Override
    public RequestOrderVO getRequestOrder(String requestOrderNo) {
        return null;
    }

    @Override
    public Object grabOrder(String requestOrderId) {

        return requestOrderMapper.grabOrder(requestOrderId);
    }

    @Override
    public Object cancelGrabOrder(String requestOrderId) {

        return requestOrderMapper.cancelGrabOrder(requestOrderId);
    }
}
